package com.syh.jn.scm.service.impl.supplier.base;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.ProjectItem;
import com.syh.jn.scm.domain.supplier.repository.base.ProjectItemRepository;
import com.syh.jn.scm.dto.supplier.base.project.ProjectItemMto;
import com.syh.jn.scm.dto.supplier.base.project.ProjectItemSto;
import com.syh.jn.scm.dto.supplier.base.project.ProjectItemVo;
import com.syh.jn.scm.service.api.supplier.base.ProjectItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运营项目关联商品
 */
@Service
@Transactional
public class ProjectItemServiceImpl extends AbstractServiceImpl<ProjectItemSto, ProjectItemMto, ProjectItemVo, ProjectItem, Long> implements ProjectItemService {
  @Autowired
  private ProjectItemRepository projectItemRepository;

  @Override
  protected AggregateRepository<ProjectItem, Long> getAggregateRepository() {
    return projectItemRepository;
  }

  /**
   * 关联商品
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(ProjectItemSto sto) {
    ExtAssert.notNull(sto, "保存对象为空");
    ValidationUtils.validate(sto);

    ProjectItem projectItem = new ProjectItem(sto);
    return projectItemRepository.add(projectItem);
  }

  /**
   * 取消关联商品
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    ExtAssert.notNull(id, "id为空");
    ProjectItem projectItem = projectItemRepository.getById(id);

    ExtAssert.notNull(projectItem, "对象为空");
    projectItem.cancelRelationItem(id);

    projectItemRepository.removeById(id);
  }

  @Override
  public void update(ProjectItemMto mto) {

  }
}
